package com.charging.business.media;

import com.charging.business.utils.ToolsUtil;

/**
 * CPC卡系统信息文件(MF/EF01)的编码信息
 * @author lzj
 */
public class CpcMfEf01EdModel {
    public static final int MSG_LEN = 30;

    /**
     * 发行方代码(1-8,8,an)样例:BAFEB1B142010001
     */
    public byte[] issuerCode = new byte[8];

    /**
     * CPC卡号(9-16,8,cn)样例:4201016100009820
     */
    public byte[] cpcCardNo = new byte[8];

    /**
     * 版本编号(17,1,n),当前为0x01
     */
    public byte[] version = new byte[1];

    /**
     * 开始日期(18-21,4,cn),格式:CCYYMMDD,样例:20191201
     */
    public byte[] startTime = new byte[4];

    /**
     * 截止日期(22-25,4,cn),格式:CCYYMMDD,样例:20291201
     */
    public byte[] endTime = new byte[4];

    /**
     * 备用(26-30,5,an)
     */
    public byte[] spare = new byte[5];

    /**
     * BIN转实体
     *
     * @param buff
     * @return
     */
    public static CpcMfEf01EdModel parseSelf(byte[] buff) {
        int index = 0;
        CpcMfEf01EdModel ret = new CpcMfEf01EdModel();

        index += ToolsUtil.arrcopy(buff, index, ret.issuerCode);
        index += ToolsUtil.arrcopy(buff, index, ret.cpcCardNo);
        index += ToolsUtil.arrcopy(buff, index, ret.version);
        index += ToolsUtil.arrcopy(buff, index, ret.startTime);
        index += ToolsUtil.arrcopy(buff, index, ret.endTime);
        index += ToolsUtil.arrcopy(buff, index, ret.spare);
        return ret;
    }

    /**
     * HEX字符串转实体
     *
     * @param strHex
     * @return
     */
    public static CpcMfEf01EdModel parse(String strHex) {
        if (strHex == null || strHex.isEmpty()) {
            return null;
        }
//        if (strHex.length() < CPCMFEF01ED.MSG_LEN * 2) return null;
        if (!ToolsUtil.chkHexChar(strHex)) {
            return null;
        }
        byte[] buff = ToolsUtil.hex2bin(strHex);
        return parseSelf(buff);
    }

    /**
     * 信息解码
     *
     * @return
     */
    public CpcMfEf01Model decoded() {
        CpcMfEf01Model dd = new CpcMfEf01Model();
        dd.issuerCode = ToolsUtil.bin2hex(issuerCode);
        dd.cpcCardNo = ToolsUtil.bin2hex(cpcCardNo);
        dd.version = ToolsUtil.bin2hex(version);
        dd.startTime = ToolsUtil.bin2hex(startTime);
        dd.endTime = ToolsUtil.bin2hex(endTime);

        return dd;
    }
}
